package cn.kshost.zqyg.manager.controller;

import cn.kshost.zqyg.manager.service.SysMenuService;
import cn.kshost.zqyg.manager.service.SysUserService;
import cn.kshost.zqyg.manager.service.ValidateCodeService;
import cn.kshost.zqyg.model.dto.h5.UserLoginDto;
import cn.kshost.zqyg.model.dto.system.LoginDto;
import cn.kshost.zqyg.model.entity.system.SysUser;
import cn.kshost.zqyg.model.vo.common.Result;
import cn.kshost.zqyg.model.vo.common.ResultCodeEnum;
import cn.kshost.zqyg.model.vo.h5.UserInfoVo;
import cn.kshost.zqyg.model.vo.system.LoginVo;
import cn.kshost.zqyg.model.vo.system.SysMenuVo;
import cn.kshost.zqyg.model.vo.system.ValidateCodeVo;
import cn.kshost.zqyg.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/index")
@Tag(name = "首页接口")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private SysMenuService sysMenuService;


    @Operation(summary = "用户登录接口")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo,200,"登录成功");
    }
    @Operation(summary = "获取验证码")
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo =  validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,200,"验证码生成成功");
    }


    @Operation(summary = "通过token获取用户信息")
    @GetMapping("/userinfo")
    public Result<UserInfoVo> getUserInfo(@RequestHeader("token") String token){
//        UserInfoVo userInfoVo =  sysUserService.getUserInfo(token);

        SysUser sysUser = AuthContextUtil.getValue();
        UserInfoVo userInfoVo = new UserInfoVo();
         userInfoVo.setAvatar(sysUser.getAvatar());
         userInfoVo.setNickName(sysUser.getName());
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "退出登录")
    @DeleteMapping("/logout")
    public Result logout(@RequestHeader("token") String token){

        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询用户自己的菜单")
    @GetMapping("/menus")
    public Result<List<SysMenuVo>> menus(){

        List<SysMenuVo>  sysMenuVoList =  sysMenuService.menus();


        return  Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }
}
